
/**   
*    
* 项目名称：a_yongan   
* 类名称：WarehouseController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月17日 下午8:25:39    
* 修改时间：2018年6月17日 下午8:25:39   
* 修改备注：   
* @version    
*    
*/
package com.yongan.production.web;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.production.entity.ProcessDetails;
import com.yongan.production.entity.ProcessProcess;
import com.yongan.production.entity.ProductionPlan;
import com.yongan.production.service.ProcessDetailsService;
import com.yongan.production.service.ProcessProcessService;
import com.yongan.production.service.ProductionPlanService;
import com.yongan.user.dto.UserResult;
import com.yongan.user.entity.Dept;
import com.yongan.user.entity.User;
import com.yongan.user.service.DeptService;
import com.yongan.user.service.UserService;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.util.SerialNumber;
import com.yongan.util.Tree;
import com.yongan.util.TreeForGoodsinformation;
import com.yongan.warehouse.entity.GoodType;
import com.yongan.warehouse.entity.Goodsinformation;
import com.yongan.warehouse.service.GoodTypeService;
import com.yongan.warehouse.service.GoodsinformationService;
import com.yongan.workflow.entity.FlowDetails;
import com.yongan.workflow.entity.PersonalWork;
import com.yongan.workflow.service.PersonalWorkService;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/processprocess")
public class ProcessProcessController {

	@Autowired
	private ProcessProcessService processProcessService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private UserService userService;
	@Autowired
	private GoodsinformationService goodsinformationService;
	@Autowired
	private GoodTypeService goodTypeService;
	@Autowired
	private ProcessDetailsService detailsService;
	@Autowired
	private ProductionPlanService productionPlanService;
	@Autowired
	private PersonalWorkService personalWorkService;
	@RequestMapping(value = "/processprocesslist", method = RequestMethod.GET)
	public String processprocesscontent(Model model,HttpServletRequest request) {
		List<ProcessProcess> processProcesslist = null;
		String page = request.getParameter("currentPage");
		String type = request.getParameter("type");
		/*if(type.equals("ftl")){
			type = "阀体类";
		}else if(type.equals("fzl")){
			type = "阀座类";
		}else if(type.equals("fgl")){
			type = "阀盖类";
		}else if(type.equals("fganl")){
			type = "阀杆类";
		}else{
			type = "";
		}*/
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<ProcessProcess> pb = new PageBean<ProcessProcess>();
		
		totalRecords = processProcessService.getCount(type);
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			processProcesslist = processProcessService.getProcessProcess(start, end,type);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("type", type);
		model.addAttribute("processprocesslist", processProcesslist);
		return "processprocesslist";
	}
	@RequestMapping(value = "/{processprocessId}/detail", method = RequestMethod.GET)
	public String processprocessdetail(@PathVariable("processprocessId") String processprocessId, Model model,HttpServletRequest request) {
		List<GoodType> goodTypeList = goodTypeService.getAllTree();
		List<Goodsinformation> goodsinformationTree = goodsinformationService.getAllGoodsinformationList();
		List<TreeForGoodsinformation> treeList = new ArrayList<TreeForGoodsinformation>();
		for(Goodsinformation goodsinformation:goodsinformationTree){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setId(goodsinformation.getGoodsinformationNumber());
			tree.setName(goodsinformation.getGoodsinformationHpmc());
			tree.setHpxh(goodsinformation.getGoodsinformationHpxh());
			tree.setDw(goodsinformation.getGoodsinformationHpdw());
			tree.setDj(goodsinformation.getGoodsinformationHpdj());
			tree.setpId(goodsinformation.getGoodsinformationHplbid());
			tree.setRid(goodsinformation.getGoodsinformationId());
			tree.setType(goodsinformation.getGoodsinformationHplb());
			treeList.add(tree);
		}
		for(GoodType goodType:goodTypeList){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setName(goodType.getGoodtypeMc());
			tree.setId(goodType.getGoodtypeId());
			tree.setNocheck(true);
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("goodinformationTree", json);
		
		List<ProductionPlan> productionplanlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<ProductionPlan> pb = new PageBean<ProductionPlan>();
		
		totalRecords = productionPlanService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			productionplanlist = productionPlanService.getProductionPlan(start, end);
			System.out.println(productionplanlist.toString());
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("productionplanlist", productionplanlist);
		
		List<Dept> deptTree = deptService.getDeptListTree();
		List<User> userTree = userService.getUserListTree();
		//JSONArray jsonObject = JSONArray.fromObject(deptTree);
		//JSONArray jsonObject1 = JSONArray.fromObject(userTree);
		
		List<Tree> treeList1 = new ArrayList<Tree>();
		for (Dept dept:deptTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(dept.getDeptId()));
			tree.setName(dept.getDeptName());
			tree.setpId(dept.getTopId());
			if(dept.getDeptId().equals("1")){
				tree.setOpen(true);
			}
			tree.setNocheck(true);
			treeList1.add(tree);
		}
		for(User user:userTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(user.getUserId()));
			tree.setName(user.getUserName());
			tree.setpId(user.getDeptId());
			treeList1.add(tree);
		}
		JSONArray jsonObject1 = JSONArray.fromObject(treeList1);
		String json1 = jsonObject1.toString();
	
		//System.out.println(json);
		/*json = json.replaceAll("deptId", "id").replaceAll("topId", "pId")
				.replaceAll("deptName", "name");*/
		model.addAttribute("allusers", json1);
		if (!(processprocessId.equals("")|processprocessId==null)) {
			ProcessProcess processProcess = processProcessService.getProcessProcessById(processprocessId);
			model.addAttribute("processprocess", processProcess);
		}
		PersonalWork pw = personalWorkService.getPersonalWork(processprocessId);
		model.addAttribute("performer", pw.getPersonalWorkUserId());
		FlowDetails fd = personalWorkService.getFlowDetail(processprocessId);
		model.addAttribute("fd", fd);
		return "processprocessdetail";
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> processprocessupdate(ProcessProcess processProcess, Model model,HttpServletRequest request) {
		String isflowwork = request.getParameter("isflowwork");
		String flowtype = request.getParameter("flowtype");
		System.out.println(flowtype+"=========="+isflowwork+"=======");
		String userid = request.getSession().getAttribute("userid").toString();
		Execution rs = processProcessService.executeProcessProcess(processProcess, processProcess.getProcessProcessId(), "123321231",isflowwork,userid,flowtype);
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	@RequestMapping(value = "/{processprocessId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> processprocessDelete(
			@PathVariable("processprocessId") String processprocessId, Model model,HttpServletRequest request) {
		if (!(processprocessId.equals("")|processprocessId==null)) {
			String userid = request.getSession().getAttribute("userid").toString();
			String isflowwork = request.getParameter("isflowwork");
			String flowtype = request.getParameter("flowtype");
			Execution rsdelete = processProcessService.executeProcessProcess(null, processprocessId, "1212331232",isflowwork,userid,flowtype);
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String processprocessadd(Model model,HttpServletRequest request) {
		String number = SerialNumber.Getnum();
		number = "JGD"+number;
		model.addAttribute("number", number);
		String type = request.getParameter("type");
		if(type.equals("ftl")){
			type = "阀体类";
		}else if(type.equals("fzl")){
			type = "阀座类";
		}else if(type.equals("fgl")){
			type = "阀盖类";
		}else if(type.equals("fganl")){
			type = "阀杆类";
		}else{
			//type = type;
		}
		model.addAttribute("type", type);
		List<ProductionPlan> productionplanlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<ProductionPlan> pb = new PageBean<ProductionPlan>();
		
		totalRecords = productionPlanService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			productionplanlist = productionPlanService.getProductionPlan(start, end);
			System.out.println(productionplanlist.toString());
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("productionplanlist", productionplanlist);
		
		List<GoodType> goodTypeList = goodTypeService.getAllTree();
		List<Goodsinformation> goodsinformationTree = goodsinformationService.getAllGoodsinformationList();
		List<TreeForGoodsinformation> treeList = new ArrayList<TreeForGoodsinformation>();
		for(Goodsinformation goodsinformation:goodsinformationTree){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setId(goodsinformation.getGoodsinformationNumber());
			tree.setName(goodsinformation.getGoodsinformationHpmc());
			tree.setHpxh(goodsinformation.getGoodsinformationHpxh());
			tree.setDw(goodsinformation.getGoodsinformationHpdw());
			tree.setDj(goodsinformation.getGoodsinformationHpdj());
			tree.setpId(goodsinformation.getGoodsinformationHplbid());
			tree.setRid(goodsinformation.getGoodsinformationId());
			tree.setType(goodsinformation.getGoodsinformationHplb());
			treeList.add(tree);
		}
		for(GoodType goodType:goodTypeList){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setName(goodType.getGoodtypeMc());
			tree.setId(goodType.getGoodtypeId());
			tree.setNocheck(true);
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("goodinformationTree", json);
		
		
		List<Dept> deptTree = deptService.getDeptListTree();
		List<User> userTree = userService.getUserListTree();
		//JSONArray jsonObject = JSONArray.fromObject(deptTree);
		//JSONArray jsonObject1 = JSONArray.fromObject(userTree);
		
		List<Tree> treeList1 = new ArrayList<Tree>();
		for (Dept dept:deptTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(dept.getDeptId()));
			tree.setName(dept.getDeptName());
			tree.setpId(dept.getTopId());
			if(dept.getDeptId().equals("1")){
				tree.setOpen(true);
			}
			tree.setNocheck(true);
			treeList1.add(tree);
		}
		for(User user:userTree){
			Tree tree = new Tree();
			tree.setId(String.valueOf(user.getUserId()));
			tree.setName(user.getUserName());
			tree.setpId(user.getDeptId());
			treeList1.add(tree);
		}
		JSONArray jsonObject1 = JSONArray.fromObject(treeList1);
		String json1 = jsonObject1.toString();
	
		//System.out.println(json);
		/*json = json.replaceAll("deptId", "id").replaceAll("topId", "pId")
				.replaceAll("deptName", "name");*/
		model.addAttribute("allusers", json1);
		return "processprocessadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> processprocessadddo(ProcessProcess processProcess, Model model,HttpServletRequest request) {
		processProcess.setProcessProcessId(UUID.randomUUID().toString());
		String isflowwork = request.getParameter("isflowwork");
		String flowtype = request.getParameter("flowtype");
		String userid = request.getSession().getAttribute("userid").toString();
		Execution rs = processProcessService.executeProcessProcess(processProcess, "", "1233213",isflowwork,userid,flowtype);
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	/**
	 * 返回产品结构清单树
	 * @param model
	 * @return
	 *//*
	@RequestMapping(value = "/{productionId}/value", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<ProductStructureList> productionvalue(
			@PathVariable("productionId") String productionId, Model model) {
		if (!(productionId.equals("")|productionId==null)) {
			ProductStructureList list = listService.getProductStructureListByGoodsId(productionId);
			if (list != null) {
				return new UserResult<ProductStructureList>(true, list);
			}else{
				return new UserResult<ProductStructureList>(false, "");
			}
		}
		return new UserResult<ProductStructureList>(false, "");
	}
	*/
	@RequestMapping(value = "/{processprocessId}/ruku", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> processprocessRuku(
			@PathVariable("processprocessId") String processprocessId, Model model) {
		if (!(processprocessId.equals("")|processprocessId==null)) {
			List<ProcessDetails> detailsall = detailsService.getProcessDetailsList(processprocessId);
			Execution rsdelete = processProcessService.executeRuku(processprocessId,detailsall, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
}
